/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.sidepanels;

import controller.Constants;
import controller.commands.EditNodeCommand;
import java.util.HashMap;
import javax.swing.JLabel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import model.graphic.objects.GNode;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Arfeniel
 */
public class RunTableModel extends AbstractTableModel implements TableModelListener {

    private String[] columnNames;
    private Object[][] data;
    private GraphicView gv;
    private HashMap<JLabel, GNode> mapping;


    RunTableModel(String[] columnNames, Object[][] data,HashMap<JLabel, GNode> mapping, GraphicView gv) {
        this.columnNames = columnNames;
        this.data = data;;
        this.gv = gv;
        this.mapping=mapping;
        this.addTableModelListener(this);

    }

    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        return true;
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel) e.getSource();
        Object datavalue = model.getValueAt(row, 0);
        JLabel l = (JLabel) datavalue;
        String naamattr = (String) model.getColumnName(column);
        if (naamattr.equals("Name")) {
            GNode gn = mapping.get(l);
            gn.setName(l.getText());
        }else if(naamattr.equals("Value")){
            GNode gn = mapping.get(l);
            Object datavalue2 = model.getValueAt(row, column);
            gv.notifyObservers(new EditNodeCommand(Constants.getValueString(), gn,(String) datavalue2));
        }
        gv.repaint();
    }
}
